﻿using LCL.Network.Enthernet;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace LCL.FileNetServer
{
    public partial class FormFileServer : Form
    {
        public FormFileServer()
        {
            InitializeComponent();
        }

        #region Advanced Server

        private AdvancedFileServer advancedFileServer;

        private void AdvancedFileServerStart()
        {
            int port;
            // textBox1.Text为端口号信息
            if (!int.TryParse( textBox1.Text, out port ))
            {
                MessageBox.Show( "Advanced文件服务器引擎的端口号输入异常" );
            }

            advancedFileServer = new AdvancedFileServer( );
            advancedFileServer.FilesDirectoryPath = textBox3.Text;                 // 所有文件存储的路径
            advancedFileServer.FilesDirectoryPathTemp = textBox4.Text;             // 临时文件的目录
            advancedFileServer.Token = new Guid( textBox2.Text );                  // 令牌
            advancedFileServer.LogNet = new LogNet.LogNetSingle( Application.StartupPath + "\\Logs\\AdvancedLog.txt" );  // 设置日志
            advancedFileServer.LogNet.BeforeSaveToFile += LogNet1_BeforeSaveToFile;
            advancedFileServer.ServerStart( port );
        }

        private void LogNet1_BeforeSaveToFile(object sender, LogNet.LogEventArgs e)
        {
            if (InvokeRequired)
            {
                Invoke(new Action<object, LogNet.LogEventArgs>(LogNet1_BeforeSaveToFile), sender, e);
                return;
            }

            textBox5.AppendText(e.HslMessage.ToString() + Environment.NewLine);
        }
        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                // 启动文件服务器
                AdvancedFileServerStart();
                button1.Enabled = false;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        #endregion

        #region Ultimate Server
        private UltimateFileServer ultimateFileServer;

        private void UltimateFileServerStart()
        {
            int port;
            if (!int.TryParse( textBox10.Text, out port ))
            {
                MessageBox.Show( "Advanced文件服务器引擎的端口号输入异常" );
            }

            ultimateFileServer = new UltimateFileServer( );
            ultimateFileServer.FilesDirectoryPath = textBox8.Text;                   // 设置文件的存储路径
            ultimateFileServer.Token = new Guid( textBox9.Text );                    // 令牌
            ultimateFileServer.LogNet = new LogNet.LogNetSingle( Application.StartupPath + "\\Logs\\UltimateLog.txt" );  // 日志
            ultimateFileServer.LogNet.BeforeSaveToFile += LogNet2_BeforeSaveToFile;
            ultimateFileServer.ServerStart( port );
        }

        private void LogNet2_BeforeSaveToFile(object sender, LogNet.LogEventArgs e)
        {
            if (InvokeRequired)
            {
                Invoke(new Action<object, LogNet.LogEventArgs>(LogNet2_BeforeSaveToFile), sender, e);
                return;
            }

            textBox6.AppendText(e.HslMessage.ToString() + Environment.NewLine);
        }
        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                // 启动服务器
                UltimateFileServerStart();
                button2.Enabled = false;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        #endregion

        private void FormFileServer_Load(object sender, EventArgs e)
        {
            textBox2.Text = Guid.Empty.ToString();
            textBox9.Text = Guid.Empty.ToString();

            textBox3.Text = Application.StartupPath + "\\AdvancedFiles";
            textBox4.Text = Application.StartupPath + "\\FileTemp";

            textBox8.Text = Application.StartupPath + "\\UltimateFiles";
        }

        private void myIcon_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                myMenu.Show();
            }
            if (e.Button == MouseButtons.Left)
            {
                this.Visible = true;
                this.WindowState = FormWindowState.Normal;
            }     
        }
        private void myIcon_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            //判断是否已经最小化于托盘 
            if (WindowState == FormWindowState.Minimized)
            {
                //还原窗体显示 
                WindowState = FormWindowState.Normal;
                //激活窗体并给予它焦点 
                this.Activate();
                //任务栏区显示图标 
                this.ShowInTaskbar = true;
                //托盘区图标隐藏 
                myIcon.Visible = false;
            }
        }
        private void 显示窗口ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (this.Visible == false) this.Visible = true;//假如当前窗口没显示则显示当前窗口 
        }
        private void 隐藏窗口ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (this.Visible == true) this.Visible = false;//假如当前窗口为显示的则隐藏窗口 
        }
        private void 退出程序ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();//关闭当前对象(即窗体) 
            Application.Exit();//通过Application类的静态方法Exit()退出应用程序 
        }
        private void FormFileServer_SizeChanged(object sender, EventArgs e)
        {
            //判断是否选择的是最小化按钮 
            if (WindowState == FormWindowState.Minimized)
            {
                //隐藏任务栏区图标 
                this.ShowInTaskbar = false;
                //图标显示在托盘区 
                myIcon.Visible = true;
            }
        }

        private void FormFileServer_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (e.CloseReason == CloseReason.UserClosing)//当用户点击窗体右上角X按钮或(Alt + F4)时 发生      
            {
                e.Cancel = true;
                this.ShowInTaskbar = false;
                this.myIcon.Icon = this.Icon;
                this.Hide();
            }    
        }
    }
}
